<?php

namespace App\Http\Controllers;

use App\Blog;
use App\User;
use Illuminate\Http\Request;
use Auth;
class BlogController extends Controller
{
	public function __construct () {
		$this->middleware('auth');
	}

	/**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
    	$this->validate ($request,[
    		'title'=>'required',
			'content'=>'required'
		]);
        $post=$request->all();
        Auth::user()->blog()->create($post);
        session()->flash ('success','发布成功！');
        return redirect ()->back();
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function show(Blog $blog)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function edit(Blog $blog)
    {
		$this->authorize ('update',$blog);
        return view('blog.edit',compact('blog'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Blog $blog)
    {
    	$this->validate ($request,[
    		'title'=>'required',
			'content'=>'required'
		]);
        $blog->title=$request->input('title');
        $blog->content=$request->input('content');
        $blog->save();
		session()->flash ('success','编辑成功！');
		return redirect ('/');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function destroy(Blog $blog)
    {
		$this->authorize ('delete',$blog);
		$blog->delete ();
		session()->flash ('success','删除成功！');
		return redirect ('/');
    }
}
